###### This script will bind daily temp info from 6 CSV's (XY's, BOM Daily, button daily max & min, HOBO daily max & min) into one dataframe

# Establish directory
in.dir = '/home1/99/jc152199/MicroclimateStatisticalDownscale/ToAggregateIntoFinalData/'
setwd(in.dir)

# Read in Raw iButton data

iB_raw = read.csv('/home1/99/jc152199/MicroclimateStatisticalDownscale/MicroclimateData/RawAirData.csv',header=T)

# Change a few column names/formats

t.iB_raw = data.frame(site=iB_raw$site, date=paste(substr(iB_raw$date,1,4),substr(iB_raw$date,6,7),substr(iB_raw$date,9,10),sep=''),airtemp=iB_raw$air)

# Read summarized BOM data and HOBO data and XY data

rawfiles = list.files(in.dir)

BOM = read.csv(paste(rawfiles[1],sep=''),header=T)
HOBO_max = read.csv(paste(rawfiles[4],sep=''), header=T)
HOBO_min = read.csv(paste(rawfiles[5],sep=''), header=T)
XY = read.csv(paste(rawfiles[7],sep=''), header=T)
XY2 = read.csv(paste(rawfiles[6], sep=''), header=T)

# Reformat date fields for HOBO dataframes

HOBO_max$year = as.numeric(substr(HOBO_max$date,1,4))
HOBO_max$month = sprintf('%02i',as.numeric(substr(HOBO_max$date,6,7)))
HOBO_max$day = sprintf('%02i',as.numeric(substr(HOBO_max$date,9,10)))

# Recreate each data frame individually, leaving only 3 rows, site, date, max/min)

t.BOM = data.frame(site=BOM$Station_Number, date=paste(BOM$Year,sprintf('%02i',BOM$Month),sprintf('%02i',BOM$Day),sep=''), micro_max=BOM$MaxVAl, micro_min=BOM$MinVal)
t.HOBO_max = data.frame(site=HOBO_max$site, date=as.numeric(as.character(paste(HOBO_max$year,HOBO_max$month,HOBO_max$day,sep=''))), micro_max = HOBO_max$maxairtemp)
t.HOBO_min = data.frame(site=HOBO_min$site, date=as.numeric(as.character(paste(HOBO_min$year,sprintf('%02i',HOBO_min$month),sprintf('%02i',HOBO_min$day),sep=''))),micro_min = HOBO_min$x)

# Format XY dataframes

t.xy = data.frame(site=XY$POINT_ID, lat=XY$LATDECIMAL, long=XY$LONGDECIMAL)
t.xy2 = data.frame(site=XY2$point_ID, lat=XY2$latdecimal, long=XY2$longdecimal)

# Bind into one

t.xy = rbind(t.xy,t.xy2)

# Create data frame for missing XY data

WU9A3xy = data.frame(site='WU9A3',lat=-16.28476505,long=145.08408764)

# Bind onto t.xy

t.xy = rbind(t.xy,WU9A3xy)

# Summarize iB to daily mins and maxs

t.min = aggregate(t.iB_raw$airtemp, by=list(site=t.iB_raw$site,date=t.iB_raw$date), FUN=min)
t.max = aggregate(t.iB_raw$airtemp, by=list(site=t.iB_raw$site,date=t.iB_raw$date), FUN=max)
names(t.min)[3] = 'micro_min'
names(t.max)[3] = 'micro_max'

iB_min_max = data.frame(site=t.min$site, date=t.min$date, micro_min=t.min$micro_min, micro_max=t.max$micro_max)

# Fucking around to combine HOBO data

list.dates = unique(c(t.HOBO_max$date, t.HOBO_min$date)) # List of dates unique between both HOBO dataframes
list.sites = unique(c(as.character(t.HOBO_max$site), as.character(t.HOBO_min$site))) # List of sites

HOBO_min_max=NULL # Null object to bind data on to

for (site in list.sites)

{

# Subset data by site

tt.HOBO_min = t.HOBO_min[which(t.HOBO_min$site==site),]
tt.HOBO_max = t.HOBO_max[which(t.HOBO_max$site==site),]

for (tdate in list.dates)

	{
	
	# Check for tdate in first dataframe
	
	if (length(which(tt.HOBO_max$date==tdate)==1))
	
		{
		
		# Check for tdata in second dataframe, if present, produce summary
		
		cat(tdate)
		
		if (length(which(tt.HOBO_min$date==tdate)==1))
		
			{
			
			# Create dataframe of site, date, micro_min, micro_max
			
			cat('\n',tdate)
			
			t.HOBO_min_max = data.frame(site=as.character(tt.HOBO_min$site[which(tt.HOBO_min$date==tdate)]),date=tdate,micro_min=tt.HOBO_min$micro_min[which(tt.HOBO_min$date==tdate)],micro_max=tt.HOBO_max$micro_max[which(tt.HOBO_max$date==tdate)])

			HOBO_min_max = rbind(HOBO_min_max,t.HOBO_min_max)
			
			}
			
		}
		
	}
	
}
	
# End Loop

# Format dates in iButton dataframe and bind to HOBO dataframe

iB_min_max$date = as.numeric(as.character(iB_min_max$date))

CTBCC = rbind(iB_min_max, HOBO_min_max)

# Format BOM data dates and bind to CTBCC data to make final dataframe

t.BOM$date = as.numeric(as.character(t.BOM$date))

# Rearrange columns

t.BOM2 = data.frame(site=t.BOM$site,date=t.BOM$date,micro_min=t.BOM$micro_min,micro_max=t.BOM$micro_max)

# Convert sites from factor to character

CTBCC$site = as.character(CTBCC$site)
t.BOM2$site = as.character(t.BOM2$site)

final = rbind(CTBCC,t.BOM2)

# Remove unnecessary XY data

t.xy$site = as.character(t.xy$site) # Format sites in XY data

t.xy$site = gsub('S3','3',t.xy$site) # Remove leading 'S' from BOM station names

list.sites = c(unique(final$site)) # List of sites to keep XY data for

xy=NULL # Null object to bind onto

for (site in list.sites) 

	{
	
	tt.xy = t.xy[which(t.xy$site==site),]
	
	xy = rbind(xy, tt.xy)
	
	}
	
# Close loop

EUSDLIBUTTxy = data.frame(site='EUSDLIBUTT', lat=-19.48506955, long=146.9710028) # Add missing site
AU1A2xy = data.frame(site='AU1A2', lat=-17.7169538, long=145.86133905) # Add missing site

xy = rbind(xy, EUSDLIBUTTxy, AU1A2xy) # Rowbind to xy
xy_final = data.frame(site=xy$site, lat=xy$long, long=xy$lat) # Switch names of lat long columns

# Remove all records from February 29

final=final[-which(substr(final$date,5,8)=='0229'),]

# Write out xy and final

write.csv(xy_final, file='xy.csv', row.names=F)
write.csv(final, file='micro_min_max.csv', row.names=F)
















